import asyncio
from random import randint
from typing import AsyncIterable

from req_http import http_get, http_get_sync
from time import perf_counter

MAX_POKEMON = 898


def get_random_pokemon_name_sync() -> str:
    pokemon_id = randint(1, MAX_POKEMON)
    pokemon_url = f'https://pokeapi.co/api/v2/pokemon/{pokemon_id}'
    pokemon = http_get_sync(pokemon_url)
    return str(pokemon['name'])


async def get_random_pokemon_name() -> str:
    pokemon_id = randint(1, MAX_POKEMON)
    pokemon_url = f'https://pokeapi.co/api/v2/pokemon/{pokemon_id}'
    pokemon = await http_get(pokemon_url)
    return str(pokemon['name'])


async def next_pokemon(total: int) -> AsyncIterable[str]:
    for _ in range(total):
        name = await get_random_pokemon_name()
        yield name


async def main() -> None:
    time_before = perf_counter()
    async for name in next_pokemon(10):
        print(name)
    print(f'Total time (yield async): {perf_counter() - time_before}')

    time_before = perf_counter()
    names = [n async for n in next_pokemon(10)]
    print(f'Total time (yield async gather): {perf_counter() - time_before}')
    print(names)
    
    time_before = perf_counter()
    for _ in range(10):
        pokemon_name = await get_random_pokemon_name()
        print(f'Pokemon name: {pokemon_name}')
    print(f'Total time (synchronous): {perf_counter() - time_before}')
    
    time_before = perf_counter()
    result = await asyncio.gather(*[get_random_pokemon_name() for _ in range(10)])
    print(f'Total time (aynchronous): {perf_counter() - time_before}')
    print(result)


if __name__ == '__main__':
    asyncio.run(main())
